package dao.server;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class TestResult {
	private int id;
	private int idTestSuite;
	private int idTestData;
	private String name;
	private String time;
	private int fails;
	private String content;
	
	public TestResult(){
		time = "";
		content = "";
	}

	public TestResult(String time, String content) {
		this.time = time;
		this.content = content;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getIdTestSuite() {
		return idTestSuite;
	}

	public void setIdTestSuite(int idTestSuite) {
		this.idTestSuite = idTestSuite;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getFails() {
		return fails;
	}

	public void setFails(int fails) {
		this.fails = fails;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getIdTestData() {
		return idTestData;
	}

	public void setIdTestData(int idTestData) {
		this.idTestData = idTestData;
	}
	public StringWriter objectToXml(DocumentBuilder docBuilder) {
		try {
			// document elements
			Document doc = docBuilder.newDocument();
			Element document = doc.createElement("document");
			doc.appendChild(document);

			// set attribute to document element
			Attr type = doc.createAttribute("type");
			type.setValue("TESTRESULT");
			document.setAttributeNode(type);

			Attr action = doc.createAttribute("action");
			action.setValue("");
			document.setAttributeNode(action);

			// head elements
			Element head = doc.createElement("head");
			document.appendChild(head);

			// id elements
			Element id = doc.createElement("id");
			id.appendChild(doc.createTextNode(String.valueOf(this.getId())));
			head.appendChild(id);

			// id_test suite elements
			Element idTestSuite = doc.createElement("id_testsuite");
			idTestSuite.appendChild(doc.createTextNode(String.valueOf(this
					.getIdTestSuite())));
			head.appendChild(idTestSuite);
			
			// id_test data elements
			Element idTestData = doc.createElement("id_testdata");
			idTestData.appendChild(doc.createTextNode(String.valueOf(this
					.getIdTestData())));
			head.appendChild(idTestData);

			// name elements
			Element name = doc.createElement("name");
			name.appendChild(doc.createTextNode(this.getName()));
			head.appendChild(name);
			
			// time elements
			Element time = doc.createElement("time");
			time.appendChild(doc.createTextNode(this.getTime()));
			head.appendChild(time);
			
			// fails elements
			Element fails = doc.createElement("fails");
			fails.appendChild(doc.createTextNode(String.valueOf(this.getFails())));
			head.appendChild(fails);

			// body elements
			Element body = doc.createElement("body");
			document.appendChild(body);
			body.appendChild(doc.createTextNode(this.getContent()));

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			transformer.transform(source, result);
			return sw;
		} catch (Exception e) {
			//e.printStackTrace();
			return null;
		}
	}
	public void xmlToObject(Document doc){
		try {	
			NodeList nodeList = doc.getElementsByTagName("head");
			Element element = (Element)nodeList.item(0);
			try{
				this.setId(Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setId(0);
			}
			try{
				this.setIdTestSuite(Integer.parseInt(element.getElementsByTagName("id_testsuite").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setIdTestSuite(0);
			}
			try{
				this.setIdTestData(Integer.parseInt(element.getElementsByTagName("id_testdata").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setIdTestData(0);
			}
			this.setName(element.getElementsByTagName("name").item(0).getTextContent());
			this.setTime(element.getElementsByTagName("time").item(0).getTextContent());			
			try{
				this.setFails(Integer.parseInt(element.getElementsByTagName("fails").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setFails(0);
			}
			//get content
			NodeList contentList = doc.getElementsByTagName("content");	
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(contentList.item(0));
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			transformer.transform(source, result);
			this.setContent(sw.toString());
			this.setContent(content.substring(38));
		} catch (Exception e) {
			//e.printStackTrace();
		}
	}
	public String addTestResult(DAO dao){
		if(dao != null)
			if(dao.addTestResult(this))
				return String.valueOf(id);
			else
				return "Create test result unsuccess, please try again!";
		return "Create test result unsuccess, please try again!";
	}
	public String updateTestResult(DAO dao){
		if(dao != null){
			TestResult tr = dao.getTestResult(this.getId());
			if(tr == null || tr.getId() == 0)
				return "Can't find the test result";
			if(dao.updateTestResult(this))
				return "Edit test result success";					
		}
		return "Update test result unsuccess, please try again!";
	}
	public String deleteTestResult(DAO dao){
		if(dao != null){
			TestResult tr = dao.getTestResult(this.getId());
			if(tr == null || tr.getId() == 0)
				return "Can't find the test result";
			if(dao.deleteTestResult(tr))
				return "Remove test result success";
		}
		return "Remove test result unsuccess, please try again!";
	}
}
